package com.zjj.fanshe.anno.book;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class TestAnnotation {

    //解析在BookStore上的@Book注解信息
    @Test
    public void testAnnotationType() {
        Class<BookStore> clazz = BookStore.class;
        Book bookAnno = clazz.getDeclaredAnnotation(Book.class);
        System.out.println("书名："+bookAnno.value());
        System.out.println("价格："+bookAnno.price());
        System.out.println("作者："+ Arrays.toString(bookAnno.authors()));
    }

    //解析在BookStore中的属性上的@Book注解信息
    @Test
    public void testAnnotationField() throws NoSuchFieldException {
        Class<BookStore> clazz = BookStore.class;
        Field nameField = clazz.getDeclaredField("storeName");
        //isAnnotationPresent() 判断是否有指定的注解
        if(nameField.isAnnotationPresent(Book.class)) {
            //getDeclaredAnnotation() 获取注解
            Book bookAnno = nameField.getDeclaredAnnotation(Book.class);
            //注解.属性
            System.out.println("书名："+bookAnno.value());
            System.out.println("价格："+bookAnno.price());
            System.out.println("作者："+ Arrays.toString(bookAnno.authors()));
        }else{
            System.out.println("当前name属性没有Book注解");
        }
    }

    //解析在BookStore中的方法上的@Book注解信息
    @Test
    public void testAnnotationMethod() throws NoSuchMethodException {
        Class<BookStore> clazz = BookStore.class;
        Method purchaseMethod = clazz.getDeclaredMethod("purchase");
        //isAnnotationPresent() 判断是否有指定的注解
        if(purchaseMethod.isAnnotationPresent(Book.class)){
            //getDeclaredAnnotation() 获取注解
            Book bookAnno = purchaseMethod.getDeclaredAnnotation(Book.class);
            //注解.属性
            System.out.println("书名："+bookAnno.value());
            System.out.println("价格："+bookAnno.price());
            System.out.println("作者："+ Arrays.toString(bookAnno.authors()));
        }else{
            System.out.println("当前purchase方法没有Book注解");
        }
    }


}
